<?php
set_include_path("../include;include");
require_once "/util/helperFunctions.php";
require_once '/model/Event.php';
require_once '/dao/SupplementDAO.php';


class Supplement {
	private $id;
	private $description;
	private $event;
	private $name;
	private $price;

	function __construct() {

	}

	/**
	 * Create a new Supplement with this static constructor call.
	 * @param $description The description of this supplement
	 * @param $event The event that this supplement is for
	 * @param $name The name of this supplement
	 * @param $price The associated price (modifier) of this supplement
	 */
	public static function createNew(string $description, Event $event, string $name, double $price) {
		$instance = new self();
		$instance->description = $description;
		$instance->event = $event;
		$instance->name = $name;
		$instance->price = $price;
		return $instance;
	}
	
	/**
	 * Returns all Supplements corresponding to the participant identified by $participantId,
	 * for the Registration identified by $registrationId.
	 * @param integer $participantId The id of the participant
	 * @param integer $registrationId The id of the registration
	 */
	public static function allSupplementsFor(Participant $participant, Registration $registration) {
		$dao = SupplementDAO::getInstance();
		
		return (array) $dao->getAllSupplementsFor($participant->getId(), $registration->getId());
	}
	
	public static function allSupplementsOf(Event $event) {
		$dao = SupplementDAO::getInstance();
		
		return (array) $dao->getAllSupplementsOf($event);
	}
	
	public static function loadSupplement(integer $id) {
		$dao = SupplementDAO::getInstance();
		
		$suppl = new Supplement();
		$suppl->load($id);
		return $suppl;
	}

	public function load(integer $id) {
		$dao = SupplementDAO::getInstance();
		
		$this->id = $id;
		$this->description = $dao->getDescriptionBySupplementId($id);
		$this->event = Event::loadEvent($dao->getEventIdBySupplementId($id));
		$this->name = $dao->getNameBySupplementId($id);;
		$this->price = $dao->getCostBySupplementId($id);;
	}

	public function save() {
		$dao = SupplementDAO::getInstance();
		
		$this->event->save();
		$myid = $dao->save($this);
		$this->id = $myid;
		return $myid;
	}

	public function getId() {
		return $this->id;
	}

	public function setId(integer $id) {
		$this->id = $id;
	}

	public function getDescription() {
		return $this->description;
	}

	public function getEvent() {
		return $this->event;
	}

	public function getName() {
		return $this->name;
	}

	public function setName(string $name) {
		$this->name = $name;
	}

	public function getPrice() {
		return $this->price;
	}

	public function setPrice(double $price) {
		$this->price = $price;
	}

	public function __toString() {
		return "Supplement: ".$this->name.", ".$this->price;
	}
}
?>